package com.william.jdbcplus.core.toolkit;

import com.william.jdbcplus.core.util.datameta.TableMetaColumnInfo;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Types;
import java.util.Date;

public class SqlTypeConvertUtil {
    public static Class sqlTypeToJavaType(int sqlType, TableMetaColumnInfo columnInfo) {
        switch (sqlType) {
            case Types.BIT:
                return Boolean.class;
            case Types.SMALLINT:
                return Integer.class;
            case Types.INTEGER:
                return Integer.class;
            case Types.BIGINT:
                return Long.class;
            case Types.FLOAT:
                return Float.class;
            case Types.REAL:
                return Double.class;
            case Types.DOUBLE:
                return Double.class;
            case Types.NUMERIC:
                if (columnInfo.getDecimalDigits() > 0) {
                    if (columnInfo.getColumnSize() > 30) {
                        return java.math.BigDecimal.class;
                    }
                    if (columnInfo.getColumnSize() <= 30 && columnInfo.getColumnSize() > 20) {
                        return Double.class;
                    }
                    if (columnInfo.getColumnSize() <= 20) {
                        return Float.class;
                    }
                } else {
                    if (columnInfo.getColumnSize() == 1) {
                        return Boolean.class;
                    }
                    if (columnInfo.getColumnSize() > 1 && columnInfo.getColumnSize() <= 10) {
                        return Integer.class;
                    }
                    if (columnInfo.getColumnSize() > 10) {
                        return Long.class;
                    }
                }
                return Integer.class;
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
            case Types.NCHAR:
            case Types.NVARCHAR:
            case Types.LONGNVARCHAR:
            case Types.NCLOB:
            case Types.CLOB:
                return String.class;
            case Types.DATE:
            case Types.TIME:
            case Types.TIMESTAMP:
                return java.util.Date.class;
            case Types.BINARY:
            case Types.BLOB:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
                return Byte[].class;
            case Types.JAVA_OBJECT:
                return Object.class;
            default:
                return String.class;
        }
    }

    public static String javeTypeToMysql(Class<?> javaClass) {
        if (Boolean.class.equals(javaClass)) {
            return "tinyint";
        } else if (Integer.class.equals(javaClass) || Short.class.equals(javaClass)) {
            return "int";
        } else if (Long.class.equals(javaClass)) {
            return "bigint";
        } else if (Float.class.equals(javaClass)) {
            return "float";
        } else if (Double.class.equals(javaClass)) {
            return "double";
        } else if (BigDecimal.class.equals(javaClass)) {
            return "decimal";
        } else if (Date.class.equals(javaClass)) {
            return "datetime";
        }
        return "varchar";
    }

    public static String javeTypeToOracle(Class<?> javaClass) {
        if (Boolean.class.equals(javaClass)) {
            return "number";
        } else if (Integer.class.equals(javaClass) || Long.class.equals(javaClass) || Short.class.equals(javaClass)) {
            return "number";
        } else if (BigInteger.class.equals(javaClass)) {
            return "number";
        } else if (Float.class.equals(javaClass) || Double.class.equals(javaClass) || BigDecimal.class.equals(javaClass)) {
            return "number";
        } else if (Date.class.equals(javaClass)) {
            return "date";
        }
        return "varchar2";
    }
}
